package day07;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author zouran
 * createDate：2024/7/3  20:49
 */
public class ServerTest {
    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(5678);
        while (true) {
            Socket socket = server.accept();
            new Thread(() -> {
                try {
                    handleTxt(socket);
                    System.out.println("保存成功");
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }
    }

    public static void handleTxt(Socket socket) throws IOException {
        OutputStream file = new FileOutputStream("D:\\Users\\13055\\Desktop\\re.txt");
        InputStream in = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int len;
        while ((len = in.read(bytes)) != -1) {
            file.write(bytes, 0, len);
        }
        in.close();
        file.close();
        socket.close();
    }
}
